<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
	include './'.$class_name . '.php';
	}
	
	function test_input($data) {
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
	
	$featured = 'N';
	$sortOrder = 'dt_listing';
	$dbConfig = new DBConfigNew(); 
	$conn = $dbConfig->getConnection();
	$mysqli=$conn;
	$results = array();
	$featuredProjInfo = array();
	$projList = array();
	$status = false;

	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		if (isset($_POST["featured"])){
			$featured = test_input($_POST["featured"]);		
		}
		if	(isset($_POST["sortOrder"])){
			$temp = $_POST["sortOrder"];
			//echo $temp;
			if($temp == 'name'){
				$sortOrder = 'tx_proj_name';
			}else if($temp == 'rating'){
				$sortOrder = 'cd_ratings';
			}
		}
		$status = true;
	}else {
		$status  = false;
	}

	if ($status && $featuredproject=$mysqli->query( "select p.id_project,p.tx_proj_name,p.tx_proj_short_desc,
								p.cd_ratings, d.tx_name from pyramid.project p
									inner join pyramid.developer d
										on p.id_developer = d.id_developer
									where ('Y' = '".$featured."' and p.in_featured = 'Y') or ('N' = '".$featured."')
										 and p.in_active = 'Y'  
										 order by ".$sortOrder." DESC,dt_listing DESC" )){
									
		while($row = $featuredproject->fetch_object()){
			$featuredProjInfo = array();	
			$featuredProjInfo['projectId'] = $row->id_project;
			$featuredProjInfo['projectName'] = $row->tx_proj_name;
			$featuredProjInfo['projectDesc'] = $row->tx_proj_short_desc;
			$featuredProjInfo['ratingStar'] = $row->cd_ratings;
			$featuredProjInfo['developerName'] = $row->tx_name;					
			array_push($projList, $featuredProjInfo);
		
		}							
		$status = true;	
		
	}else {
		$status = false;
	}
	
	$dbConfig -> closeConnection();

	
	if($status){
		$results['projects'] = $projList;
	}
	$results['success'] = $status;

	
	echo json_encode($results);
	ob_end_flush();
?>